unit CreateUpdateInfo;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, DBCtrlsEh, ExtCtrls,FileCtrl;


  procedure ClearUpdateinfo;
  function GetUpdateInfo(CurrentDir:string):TStringList;
  procedure RecurSearchFile(CurrentDir:string;SearchFileType:string;SearchResult:TStrings;var Number:integer);
  function CurrentIsValidDir(SearchRec:TSearchRec):integer;
  function AppPath: string;

var
  TotalFileNumbers:Integer;
  SearchFileType:String;
  gCurrentDir:string='';

implementation

function getTempFile:string;
Var
  Buf:string[40];
  Pbuf:Pchar;
  TmpFileName:String[40];
  PFile:PChar;
begin
  PBuf:=@buf;
  pFile := @TmpFileName;
  GetTempPath(40,Pbuf);
  GetTEmpFileName(PBuf,'',0,PFile);
  result := string(PFile);
end;

function CovFileDate(Fd:_FileTime):TDateTime;
var
Tct:_SystemTime;
Temp:_FileTime;
begin
  FileTimeToLocalFileTime(Fd,Temp);
  FileTimeToSystemTime(Temp,Tct);
  CovFileDate:=SystemTimeToDateTime(Tct);
end;

function CurrentIsValidDir(SearchRec: TSearchRec): integer;
begin
  if ( (SearchRec.Attr <> 16) and(SearchRec.Name<>'.')  and(SearchRec.Name<>'..') ) then
    Result:=0
  else if ( ((searchrec.Attr=16)) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') ) then
    Result:=1
  else if (searchrec.Attr=18) then
    result:=1
  else
    Result:=2;
end;

procedure RecurSearchFile(CurrentDir, SearchFileType: string;
  SearchResult: TStrings; var Number: integer);
var
 i:integer;
 Subdir:TStringList;
 SearchRec:TsearchRec;
 shuxin:string;
 FmtSets: TFormatSettings;
begin
  if (FindFirst(CurrentDir+SearchFileType, faAnyFile, SearchRec)=0) then
  begin
    repeat
      if CurrentIsValidDir(SearchRec)=0 then
      begin
        GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, FmtSets);
        if CurrentDir = gCurrentDir then
        begin
          if FmtSets.DateSeparator='-' then
            Searchresult.Add(SearchRec.Name+';'+FormatDateTime('yyyy-mm-dd HH:mm',CovFileDate(SearchRec.FindData.ftLastWriteTime)))
          else if FmtSets.DateSeparator='/' then
            Searchresult.Add(SearchRec.Name+';'+FormatDateTime('yyyy-mm-dd HH:mm',CovFileDate(SearchRec.FindData.ftLastWriteTime)))
          else if FmtSets.DateSeparator='.' then
            Searchresult.Add(SearchRec.Name+';'+FormatDateTime('yyyy-mm-dd HH:mm',CovFileDate(SearchRec.FindData.ftLastWriteTime)))
          else
            Searchresult.Add(SearchRec.Name+';'+FormatDateTime('yyyy-mm-dd HH:mm',CovFileDate(SearchRec.FindData.ftLastWriteTime)));
        end
        else begin
          if FmtSets.DateSeparator='-' then
            Searchresult.Add(CurrentDir+SearchRec.Name+';'+FormatDateTime('yyyy-mm-dd HH:mm',CovFileDate(SearchRec.FindData.ftLastWriteTime)))
          else if FmtSets.DateSeparator='/' then
            Searchresult.Add(CurrentDir+SearchRec.Name+';'+FormatDateTime('yyyy-mm-dd HH:mm',CovFileDate(SearchRec.FindData.ftLastWriteTime)))
          else if FmtSets.DateSeparator='.' then
            Searchresult.Add(CurrentDir+SearchRec.Name+';'+FormatDateTime('yyyy-mm-dd HH:mm',CovFileDate(SearchRec.FindData.ftLastWriteTime)))
          else
            Searchresult.Add(CurrentDir+SearchRec.Name+';'+FormatDateTime('yyyy-mm-dd HH:mm',CovFileDate(SearchRec.FindData.ftLastWriteTime)));
        end;
      end;
      application.ProcessMessages ;
    until (FindNext(SearchRec) <> 0);
  end;
  FindClose(SearchRec);

  Subdir:=TStringList.Create;
  if (FindFirst(CurrentDir+'*.*', faDirectory, SearchRec)=0) then
  begin
   repeat
     if CurrentIsValidDir(SearchRec)=1 then
     begin
       if SearchRec.Name = 'Report' then
         Subdir.Add(SearchRec.Name);
     end;
     application.ProcessMessages ;
   until (FindNext(SearchRec) <> 0);
  end;
  FindClose(SearchRec);

  for i:=0 to Subdir.Count-1 do
    RecurSearchfile(Subdir.Strings[i]+'\',SearchFileType,Searchresult,Number);

  Subdir.Free;
end;

procedure ClearUpdateinfo;
begin
  TotalFileNumbers:=0;
  SearchFileType:='*.*';
end;

function AppPath: string;
begin
  Result := ExtractFilePath(Application.ExeName);
end;

function GetUpdateInfo(CurrentDir:string):TStringList;
var SelectDir:string;
UpdateInfo:TStringList;
begin
  gCurrentDir := AppPath;
  UpdateInfo:=TStringList.Create;
  TotalFileNumbers:=0;
  SearchFileType:='*.*';
  RecurSearchFile(gCurrentDir,SearchFileType,UpdateInfo, TotalFileNumbers);
  UpdateInfo.SaveToFile('UpdateInfo.txt');
  result := UpdateInfo;
end;

end.
